package com.fsnbigdata.service.impl;

import com.fsnbigdata.repository.*;
import com.fsnbigdata.service.api.StatisticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional(readOnly = true)
public class StatisticServiceImpl implements StatisticService{

    @Autowired
    private OrganismRepository organismRepository;

    @Autowired
    private SpecimenRepository specimenRepository;

    @Autowired
    private ParkRepository parkRepository;

    @Autowired
    private EcosystemRepository ecosystemRepository;

    @Autowired
    private PlantConservationRepository plantConservationRepository;

    @Autowired
    private AnimalConservationRepository animalConservationRepository;

    @Autowired
    private EndangeredSpeciesRepository endangeredSpeciesRepository;

    @Autowired
    private VegetationRepository vegetationRepository;

    @Autowired
    private ProductRepository productRepository;

    @Autowired
    private EnterpriseRepository enterpriseRepository;

    @Cacheable("statistic_count")
    @Override
    public Map<String, Long> countAllDatabase() {
        Map<String, Long> ret = new HashMap<>();

        ret.put("organism", organismRepository.count());
        ret.put("specimen", specimenRepository.count());
        ret.put("park", parkRepository.count());
        ret.put("ecosystem", ecosystemRepository.count());
        ret.put("es_cs", plantConservationRepository.count() + animalConservationRepository.count() + endangeredSpeciesRepository.count());
        ret.put("vegetation", vegetationRepository.count());
        ret.put("product", productRepository.count());
        ret.put("enterprise", enterpriseRepository.count());

        return ret;
    }



    @Cacheable("creation_count")
    @Override
    public Map<String, Long> countByCreator(String creator) {
        Map<String, Long> ret = new HashMap<>();

        ret.put("organism", organismRepository.countByCreator(creator));
        ret.put("specimen", specimenRepository.countByCreator(creator));
        //ret.put("park", parkRepository.count());
        ret.put("ecosystem", ecosystemRepository.countByCreator(creator));
        //ret.put("es_cs", plantConservationRepository.count() + animalConservationRepository.count() + endangeredSpeciesRepository.count());
        //ret.put("vegetation", vegetationRepository.count());
        //ret.put("product", productRepository.count());
        //ret.put("enterprise", enterpriseRepository.count());

        return ret;
    }

    @Override
    public List findAllByKeyWords(String key, Pageable pageable) {
        List list = new ArrayList(  );
        list.add( organismRepository.getCountByKey( key ) );
        list.add( organismRepository.findAllByKeyWords( key, pageable) );
        return list;
    }

    @Override
    public String count() {
        Long sum = getCount( organismRepository.getCount() )
                + getCount( specimenRepository.getCount() )
                + getCount( ecosystemRepository.getCount() )
                + getCount( vegetationRepository.getCount() )
                + getCount( animalConservationRepository.getCount() )
                + getCount( parkRepository.getCount() )
                + getCount( plantConservationRepository.getCount() )
                + getCount( endangeredSpeciesRepository.getCount() )
                + getCount( productRepository.getCount() )
                + getCount( enterpriseRepository.getCount() );
        return sum+"";
    }

    public Long getCount(List<Object> objects){
        Long sum = 0L;
        for (Object o:objects){
            Object[] objects1 = (Object[]) o;
            for (Integer i = 0; i < objects1.length; i++ ){
                if(objects1[i]==null||"".equals( objects1[i]+"" )){
                    continue;
                }
                sum++;
            }
        }
        return sum;
    }

}
